package com.tuo51.observation.service.impl;

import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Dimension;
import com.tuo51.evaluate.repositories.evaluatev2.EvaluateV2DimensionRepository;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.observation.domain.ObservatePoint;
import com.tuo51.observation.repositories.ObservatePointRepository;
import com.tuo51.observation.service.ObImportService;
import com.tuo51.observation.vo.observation.ObservationOptionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ObImportServiceImpl implements ObImportService {

    @Autowired
    private EvaluateV2DimensionRepository dimensionRepository;

    @Autowired
    private ObservatePointRepository observatePointRepository;

    @Override
    public Boolean importParentObservers(String channel, MultipartFile file){
        if (file==null) {
            throw  new BusinessException("12345", "file 不能为空");
        }

        try {
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());

            int sheetCount = workbook.getNumberOfSheets();

            for (int i = 0; i < sheetCount; i++) {
                XSSFSheet sheet = workbook.getSheetAt(i);
                String sheetName = sheet.getSheetName();

                Integer month = Integer.parseInt(sheetName.replaceAll("月龄",""));

                //获取多少行
                int rows = sheet.getPhysicalNumberOfRows();

            }
        } catch (IOException e) {
            throw new BusinessException("12345", "错误");
        }
        return true;
    }

    @Override
    public Boolean importTeacherObservers(String channel, MultipartFile file){
        if (file==null) {
            throw  new BusinessException("12345", "file 不能为空");
        }

        try {
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());

            int sheetCount = workbook.getNumberOfSheets();


            List<EvaluateV2Dimension> dimensions = dimensionRepository.findAllByDeletedOrderByCode(false);
            Map<String, EvaluateV2Dimension> dimensionCache = dimensions.stream().collect(Collectors.toMap(EvaluateV2Dimension::getName, o -> o));

            Set<ObservatePoint> pointsToSave = new HashSet<>();

            for (int i = 0; i < sheetCount; i++) {
                XSSFSheet sheet = workbook.getSheetAt(i);

                XSSFRow firstRow = sheet.getRow(0);

                //获取多少行

                int c = 0;
                while (c < firstRow.getLastCellNum()){
                    Integer month = (int) firstRow.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getNumericCellValue();

                    //37	观察维度	教具	问题
                    log.info("START WITH MONTH " + month + " ");

                    int rows = sheet.getPhysicalNumberOfRows();
                    int r = 1; // 第一行第二行 标题,
                    for (; r < rows; r ++){
                        XSSFRow row = sheet.getRow(r);

                        int indexOnPaper = (int) row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getNumericCellValue();
                        String dimensionName = row.getCell(c+1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                        String tools = row.getCell(c+2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                        String question = row.getCell(c+3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();

                        log.info("parsing line " + indexOnPaper + " " + dimensionName + " " + tools + " " + question + " ");

                        if (indexOnPaper == 0 || StringUtils.isAnyEmpty(dimensionName, question)){
                            continue;
                        }
                        //创建对象
                        EvaluateV2Dimension dimension = dimensionCache.get(dimensionName);

                        ObservatePoint obp = new ObservatePoint();

                        obp.setMonth(month);
                        obp.setCreated(new Date());
                        obp.setIndexOnPaper(indexOnPaper);
                        obp.setTitle(question);
                        obp.setTools(tools);
                        if (dimension != null){
                            obp.setDimensionId(dimension.getId());
                        }
                        obp.setDimensionName(dimensionName);

                        ObservationOptionVO op1 = ObservationOptionVO.of("1", "通过", 3);
                        ObservationOptionVO op2 = ObservationOptionVO.of("2", "尝试中", 2);
                        ObservationOptionVO op3 = ObservationOptionVO.of("3", "不通过", 1);

                        obp.setOptions(Arrays.asList(op1,op2,op3));

                        pointsToSave.add(obp);
                    }

                    c += 4;
                }

            }

            observatePointRepository.saveAll(pointsToSave);
        } catch (IOException e) {
            throw new BusinessException("12345", "错误");
        }
        return true;
    }
}
